package com.cuixk.basic.quartz.manager.service.impl;

import com.cuixk.basic.quartz.manager.bean.QuartzJob;
import com.cuixk.basic.quartz.manager.bean.QuartzTrigger;
import com.cuixk.basic.quartz.manager.service.ScheduleTaskService;
import com.cuixk.basic.quartz.manager.util.ScheduleUtil;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Create By Cuixk
 * 2019/6/21 15:03
 */
@Service
public class ScheduleTaskServiceImpl implements ScheduleTaskService {

    @Resource
    SchedulerFactoryBean schedulerFactoryBean;

    @Override
    public int createScheduleTask(QuartzJob job, QuartzTrigger trigger) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        JobDetail jobDetail = ScheduleUtil.buildJonDetail(job);
        CronTrigger cronTrigger = ScheduleUtil.buildCronTrigger(trigger);

        scheduler.start();
        scheduler.scheduleJob(jobDetail, cronTrigger);

        return 1;
    }

    @Override
    public int updateScheduleTask(QuartzJob job, QuartzTrigger trigger) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        JobKey jobKey = JobKey.jobKey(job.getJobName(), job.getJobGroup());
        scheduler.deleteJob(jobKey);

        JobDetail jobDetail = ScheduleUtil.buildJonDetail(job);
        CronTrigger cronTrigger = ScheduleUtil.buildCronTrigger(trigger);

        scheduler.start();
        scheduler.scheduleJob(jobDetail, cronTrigger);
        return 1;
    }

    @Override
    public int pauseScheduleTask(QuartzJob job) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        JobKey jobKey = JobKey.jobKey(job.getJobName(), job.getJobGroup());
        scheduler.pauseJob(jobKey);
        return 1;
    }

    @Override
    public int reRunScheduleTask(QuartzTrigger trigger) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        TriggerKey triggerKey = TriggerKey.triggerKey(trigger.getTriggerName(), trigger.getTriggerGroup());
        CronTrigger cronTrigger = ScheduleUtil.buildCronTrigger(trigger);
        scheduler.rescheduleJob(triggerKey, cronTrigger);
        return 1;
    }


    @Override
    public List<QuartzJob> listScheduleTask() throws SchedulerException {
        List<QuartzJob> jobDetails = new ArrayList<>();
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.anyGroup());
        for (JobKey jobKey : jobKeys) {
            JobDetailImpl jobDetail = (JobDetailImpl) scheduler.getJobDetail(jobKey);
            QuartzJob quartzJob = new QuartzJob();
            quartzJob.setJobName(jobDetail.getName());
            quartzJob.setJobGroup(jobDetail.getGroup());
            JobDataMap jobDataMap = jobDetail.getJobDataMap();
            for (String key : jobDataMap.keySet()) {
                String jobValue = jobDataMap.getString(key);
                quartzJob.setJobKey(key);
                quartzJob.setJobValue(jobValue);
            }

            jobDetails.add(quartzJob);
        }
        return jobDetails;
    }

    @Override
    public int deleteScheduleTask(String jobName, String jobGroup) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        ;
        boolean deleteJob = scheduler.deleteJob(jobKey);
        return 0;
    }
}
